//Page.js
//deprecated。see hooks/usePage.js、hooks/useDialogForm.js
export default {
    data() {
        return {
            dataList: [],
            pageIndex: 1,
            pageSize: 10,
            totalPage: 0,
            dataListLoading: false,
            addOrUpdateVisible: false,
            dataListSelections: []
        }
    },
    methods: {
        // 每页数
        sizeChangeHandle(val) {
            this.pageSize = val
            this.pageIndex = 1
            this.getDataList()
        },
        // 当前页
        currentChangeHandle(val) {
            this.pageIndex = val
            this.getDataList()
        },
        // 多选
        selectionChangeHandle(val) {
            this.dataListSelections = val
        },
        // 新增 / 修改
        addOrUpdateHandle(id) {
            this.addOrUpdateVisible = true
            this.$nextTick(() => {
                this.$refs.addOrUpdate.init(id)
            })
        },
        doPromiseGetList(url, params) {
            return this.$http({
                url: this.$http.adornUrl(url),
                method: 'get',
                params: this.$http.adornParams(params)
            })
        },
        doGetList(url, params) {
            this.dataListLoading = true
            this.doPromiseGetList(url, params).then(({data}) => {
                if (data && data.code === 0) {
                    data = data.data;
                    this.dataList = data.page.list
                    this.totalPage = data.page.totalCount
                } else {
                    this.dataList = []
                    this.totalPage = 0
                }
                this.dataListLoading = false
            })
        },
        doDelete(url, id) {
            if (!url || url.trim() === '') {
                return;
            }
            let ids = id ? [id] : this.dataListSelections.map(item => {
                return item.id
            })

            this.$confirm(`确定对[id=${ids.join(',')}]进行[${id ? '删除' : '批量删除'}]操作?`, '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(() => {
                this.$http({
                    url: this.$http.adornUrl(url),
                    method: 'post',
                    data: this.$http.adornData(ids, false)
                }).then(({data}) => {
                    if (data && data.code === 0) {
                        this.$message({
                            message: '操作成功',
                            type: 'success',
                            duration: 1500,
                            onClose: () => {
                                this.getDataList()
                            }
                        })
                    } else {
                        this.$message.error(data.msg)
                    }
                })
            })
        }
    }
}